package pl.peterjack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.jdom.Element;

import pl.peterjack.model.PMF;
import pl.peterjack.model.dane.Firma;
import pl.peterjack.model.dane.PracownikUprawnienia;
import pl.peterjack.model.dane.Produkt;

import com.google.appengine.api.datastore.Key;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class SystemTools {

	private static NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("PL"));

	/**
	 * Rozni sie od standardowego Float.parseFloat - nie wywala wyjatkow - w
	 * przypadku bledu zwraca 0
	 * 
	 * @param s
	 * @return
	 */
	public static float parseFloat(String s) {
		float number = 0;
		try {
			number = Float.parseFloat(s);
		} catch (Exception e) {
			// nothing to do
		}
		return number;
	}

	/**
	 * Rozni sie od standardowego Integer.parseInteger - nie wywala wyjatkow - w
	 * przypadku bledu zwraca 0
	 * 
	 * @param s
	 * @return
	 */
	public static int parseInteger(String s) {
		int number = 0;
		try {
			number = Integer.parseInt(s);
		} catch (Exception e) {
			// nothing to do
		}
		return number;
	}

	/**
	 * Rozni sie od standardowego Long.parseLong - nie wywala wyjatkow - w
	 * przypadku bledu zwraca 0
	 * 
	 * @param s
	 * @return
	 */
	public static long parseLong(String s) {
		long number = 0;
		try {
			number = Long.parseLong(s);
		} catch (Exception e) {
			// nothing to do
		}
		return number;
	}

	public static void absText(String text, int x, int y, PdfWriter writer,
			BaseFont bf) {
		try {
			PdfContentByte cb = writer.getDirectContent();
			cb.saveState();
			cb.beginText();
			cb.moveText(x, y);
			cb.setFontAndSize(bf, 12);
			cb.showText(text);
			cb.endText();
			cb.restoreState();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void absText(String text, int x, int y, PdfWriter writer,
			BaseFont bf, int alignment) {
		try {
			PdfContentByte cb = writer.getDirectContent();
			// cb.saveState();
			cb.beginText();
			cb.setFontAndSize(bf, 12);
			cb.showTextAligned(alignment, text, x, y, 0);
			cb.endText();
			// cb.restoreState();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * To convert the InputStream to String we use the Reader.read(char[]
	 * buffer) method. We iterate until the Reader return -1 which means there's
	 * no more data to read. We use the StringWriter class to produce the
	 * string.
	 */
	public static String convertStreamToString(InputStream is) throws IOException {

		if (is != null) {
			Writer writer = new StringWriter();

			char[] buffer = new char[1024];
			try {
				Reader reader = new BufferedReader(new InputStreamReader(is,
						"UTF-8"));
				int n;
				while ((n = reader.read(buffer)) != -1) {
					writer.write(buffer, 0, n);
				}
			} finally {
				is.close();
			}
			return writer.toString();
		} else {
			return "";
		}
	}
	
	public static String nullToEmptyString(String text) {
		return (text == null) ? "" : text;  
	}
	
	public static int maxValueFromArray(int[] data) {
		if(data.length<1) {
			return 0; 
		}	
		int max = 0;
		max = data[0];
		for (int i=1; i<data.length; i++) {
		  if (data[i]>max) {
		    max = data[i];
		  }
		}
		return max; 
	}
	
	public static List<Produkt> produktyFirmy(Firma firma) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Produkt> results = null; 
	    Query query = pm.newQuery(Produkt.class, "firma == firmaPodana");
		query.declareParameters(Key.class.getName() + " firmaPodana");
		results = (List<Produkt>) query.execute(firma.getId());
		return results; 
	}
	
	public static List<PracownikUprawnienia> uprawnieniaFirmy(Firma firma) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<PracownikUprawnienia> results = null; 
	    Query query = pm.newQuery(PracownikUprawnienia.class, "firma == firmaPodana");
		query.declareParameters(Key.class.getName() + " firmaPodana");
		results = (List<PracownikUprawnienia>) query.execute(firma.getId());
		return results; 
	}
	
	public static String nullToString(String obiekt) {
		if (obiekt == null) {
			return "";
		} else {
			return obiekt; 
		}
	}
	
	public static String nullToString(Integer obiekt) {
		if (obiekt == null) {
			return "";
		} else {
			return obiekt + "";
		}
	}

	public static String nullToString(Object obiekt) {
		if (obiekt == null) {
			return "";
		} else {
			return obiekt.toString();
		}
	}

	public static String money(Double obiekt) {
		if (obiekt == null) {
			return "";
		} else {
			return formatter.format(Math.round(obiekt * 100) / 100.0).replace("¤", "");
		}
	}

	public static String money(Float obiekt) {
		if (obiekt == null) {
			return "";
		} else {
			return formatter.format(Math.round(obiekt * 100) / 100.0).replace("¤", "");
		}
	}

	public static boolean empty(String obiekt) {
		return obiekt == null || "".equals(obiekt); 
	}

	public static int getXValueFromElement(Element element) {
		String wartosc = element.getChildText("x");
		return Integer.parseInt(wartosc);
	}

	public static int getYValueFromElement(Element element) {
		String wartosc = element.getChildText("y");
		return Integer.parseInt(wartosc);
	}

}
